package com.dog.modules.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dog.framework.core.constant.Constant;
import com.dog.framework.core.exception.ServiceException;
import com.dog.framework.core.model.PageResult;
import com.dog.framework.mybatis.service.impl.BaseServiceImpl;
import com.dog.framework.web.util.SecurityUserUtils;
import com.dog.modules.system.convert.SysUserConvert;
import com.dog.modules.system.domain.SysUser;
import com.dog.modules.system.api.query.SysRoleUserQuery;
import com.dog.modules.system.api.query.SysUserQuery;
import com.dog.modules.system.api.vo.SysUserVO;
import com.dog.modules.system.enums.SuperAdminEnum;
import com.dog.modules.system.mapper.SysUserMapper;
import com.dog.modules.system.service.SysUserPostService;
import com.dog.modules.system.service.SysUserRoleService;
import com.dog.modules.system.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuxk
 * @description 针对表【sys_user(用户管理)】的数据库操作Service实现
 * @createDate 2024-01-30 14:47:29
 */
@Service
@AllArgsConstructor
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    private final SysUserRoleService sysUserRoleService;
    private final SysUserPostService sysUserPostService;

    @Override
    public PageResult<SysUserVO> page(SysUserQuery query) {
        // 查询参数
        Map<String, Object> params = getParams(query);

        // 分页查询
        IPage<SysUser> page = getPage(query);
        params.put(Constant.PAGE, page);

        // 数据列表
        List<SysUser> list = baseMapper.getList(params);

        return new PageResult<>(SysUserConvert.INSTANCE.convertList(list), page.getTotal());
    }

    private Map<String, Object> getParams(SysUserQuery query) {
        Map<String, Object> params = new HashMap<>();
        params.put("username", query.getUsername());
        params.put("mobile", query.getMobile());
        params.put("email", query.getEmail());
        params.put("orgId", query.getOrgId());
        params.put("tenantId", query.getTenantId());
        params.put("status", query.getStatus());

        // 数据权限
        params.put(Constant.DATA_SCOPE, getDataScope("t1", null));

        return params;
    }


    @Override
    public List<SysUserVO> list(SysUserQuery query) {
        // 查询参数
        Map<String, Object> params = new HashMap<>();
        params.put("username", query.getUsername());
        params.put("mobile", query.getMobile());
        params.put("email", query.getEmail());
        params.put("orgId", query.getOrgId());
        params.put("tenantId", query.getTenantId());
        params.put("status", query.getStatus());
        // 数据列表
        List<SysUser> list = baseMapper.getList(params);

        return SysUserConvert.INSTANCE.convertList(list);
    }



    @Override
    public Boolean save(SysUserVO vo) {
        SysUser entity = SysUserConvert.INSTANCE.convert(vo);
        entity.setSuperAdmin(SuperAdminEnum.NO.getValue());

        // 判断用户名是否存在
        SysUser user = baseMapper.getByUsername(entity.getUsername());
        if (user != null) {
            throw new ServiceException("用户名已经存在");
        }

        // 判断手机号是否存在
        user = baseMapper.getByMobile(entity.getMobile());
        if (user != null) {
            throw new ServiceException("手机号已经存在");
        }

        // 密码加密
        entity.setPassword(SecurityUserUtils.encryptPassword(entity.getPassword()));

        // 保存用户
        baseMapper.insert(entity);

        // 保存用户角色关系
        sysUserRoleService.saveOrUpdate(entity.getId(), vo.getRoleIdList());

        // 更新用户岗位关系
        sysUserPostService.saveOrUpdate(entity.getId(), vo.getPostIdList());

        return Boolean.TRUE;
    }

    @Override
    public void update(SysUserVO vo) {
        SysUser entity = SysUserConvert.INSTANCE.convert(vo);

        // 判断用户名是否存在
        SysUser user = baseMapper.getByUsername(entity.getUsername());
        if (user != null && !user.getId().equals(entity.getId())) {
            throw new ServiceException("用户名已经存在");
        }

        // 判断手机号是否存在
        user = baseMapper.getByMobile(entity.getMobile());
        if (user != null && !user.getId().equals(entity.getId())) {
            throw new ServiceException("手机号已经存在");
        }

        // 更新用户
        updateById(entity);

        // 更新用户角色关系
        sysUserRoleService.saveOrUpdate(entity.getId(), vo.getRoleIdList());

        // 更新用户岗位关系
        sysUserPostService.saveOrUpdate(entity.getId(), vo.getPostIdList());

        // 更新用户缓存权限
       // sysUserTokenService.updateCacheAuthByUserId(entity.getId());
    }

    @Override
    public void delete(List<Long> idList) {
        // 删除用户
        removeByIds(idList);

        // 删除用户角色关系
        sysUserRoleService.deleteByUserIdList(idList);

        // 删除用户岗位关系
        sysUserPostService.deleteByUserIdList(idList);
    }

    @Override
    public SysUserVO getByMobile(String mobile) {
        SysUser user = baseMapper.getByMobile(mobile);

        return SysUserConvert.INSTANCE.convert(user);
    }

    @Override
    public SysUserVO getByUsername(String username) {
        SysUser user = baseMapper.getByUsername(username);

        return SysUserConvert.INSTANCE.convert(user);
    }

    @Override
    public void updatePassword(Long id, String newPassword) {
        // 修改密码
        SysUser user = getById(id);
        user.setPassword(newPassword);
        updateById(user);
    }

    @Override
    public PageResult<SysUserVO> roleUserPage(SysRoleUserQuery query) {
        // 查询参数
        Map<String, Object> params = getParams(query);
        params.put("roleId", query.getRoleId());

        // 分页查询
        IPage<SysUser> page = getPage(query);
        params.put(Constant.PAGE, page);

        // 数据列表
        List<SysUser> list = baseMapper.getRoleUserList(params);

        return new PageResult<>(SysUserConvert.INSTANCE.convertList(list), page.getTotal());
    }

    @Override
    public void importByExcel(MultipartFile file, String password) {

    }

    @Override
    public void export() {

    }
}




